import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tiktokadult/hj_page/mine_business/welfare/widget/task_widgets.dart';
import 'package:tiktokadult/hj_utils/api_service/common_service.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../hj_model/mine/task_center_data.dart';
import '../../../tools_base/refresh/pull_refresh.dart';
import '../widgets/integral_exchange_view.dart';

class WelfareViewTask extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WelfareViewTaskState();
  }
}

class _WelfareViewTaskState extends State<WelfareViewTask> {
  int shareCount = 0;
  List<DailyTask>? taskList;
  bool isLoading = true;
  RefreshController? refreshController;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _initTaskList();
    });
  }

  ///初始化任务列表
  void _initTaskList() async {
    final result = await CommonService.getMyTaskList();
    taskList ??= [];
    taskList?.clear();

    result?.dailyTask?.forEach((element) {
      element.doType = 1;
    });
    taskList?.addAll(result?.dailyTask ?? []);

    result?.growthTasks?.forEach((element) {
      element.doType = 3;
    });
    taskList?.addAll(result?.growthTasks ?? []);

    result?.onceTask?.forEach((element) {
      element.doType = 2;
    });
    taskList?.addAll(result?.onceTask ?? []);

    refreshController?.refreshCompleted();
    isLoading = false;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) return LoadingCenterWidget();
    return Container(
      child: pullYsRefresh(
        onInit: (ctr) => refreshController = ctr,
        enablePullUp: false,
        onRefresh: (ctr) async {
          _initTaskList();
        },
        child: CustomScrollView(
          slivers: [
            SliverPadding(
              padding: EdgeInsets.only(left: 0, right: 0, top: 12, bottom: 12),
              sliver: SliverToBoxAdapter(
                child: TaskCenterVIPGuideNewView(),
              ),
            ),
            1.sliverLine,
            SliverToBoxAdapter(
              child: MineTaskView(taskList ?? []),
            ),
            SliverToBoxAdapter(
              child: Container(
                padding: EdgeInsets.only(top: 20, left: 18.w, right: 18.w),
                child: IntegralExchangeView(titleDesc: "积分兑换"),
              ),
            ),
            // SliverToBoxAdapter(
            //   child: TaskVIPVideosView(),
            // )
          ],
        ),
      ),
    );
  }
}
